package com.jzdjt.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jzdjt.entity.PaperEntity;
import com.jzdjt.tool.DBUtil;

import net.sf.json.JSONArray;

public class GetPaperContentServlet extends HttpServlet {

    public GetPaperContentServlet() {
        super();
    }

    @Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    	 response.setContentType("text/html;charset=utf-8"); 
         response.setCharacterEncoding("UTF-8"); 
        
         //根据标示名获取JSP文件中表单所包含的参数  
         String papertype=request.getParameter("paperType");
         String paperid=request.getParameter("paperId");
          String  result = getPaperContent(papertype,paperid);  
         
         
         PrintWriter out = response.getWriter();//回应请求
          
         out.write(result);
         out.flush();
         out.close();
         System.out.println(result);  
        
    }

    @Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { 
    	 doGet(request, response); 
         //设置客户端的解码方式为utf-8
    	 
    	 response.setContentType("text/html;charset=utf-8"); 
         response.setCharacterEncoding("UTF-8"); 
        
         //根据标示名获取JSP文件中表单所包含的参数  
         String papertype=request.getParameter("paperType");
         String paperid=request.getParameter("paperId");
          String  result = getPaperContent(papertype,paperid);  
         
         
         PrintWriter out = response.getWriter();//回应请求
          
         out.write(result);
         out.flush();
         out.close();
         System.out.println(result);  
    }

    
    
    private String getPaperContent(String papertype,String paperid) {
    	try {
    		JSONArray array = new JSONArray();
    		if(paperid==null||paperid.trim().equals("")){
                return "";
            }
            else {
            	String getPaperSql = "select * from PaperInfo where PaperID = '"+paperid+"' ";
            	DBUtil dbUtil = new DBUtil();
                List<PaperEntity> paper = dbUtil.QueryFromPaper(getPaperSql);
                array =  JSONArray.fromObject(paper);
            } 
            return array.toString();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return "";
		}
    	
	}
}